package com.dark.cms.base.utils;

import com.dark.cms.base.dto.PageDTO;

/**
 * 分页工具类
 * @author Devil
 */
public class PageUtil {
	public static PageDTO createPage(int everyPage, long totalCount, int currentPage) {
		everyPage = getEveryPage(everyPage);
		currentPage = getCurrentPage(currentPage);
		int totalPage = getTotalPage(everyPage, totalCount);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(currentPage, totalPage);
		return new PageDTO(everyPage, totalPage, totalCount, currentPage,
				beginIndex, hasNextPage, hasPrePage);

	}

	private static int getEveryPage(int everyPage) {
		return everyPage == 0 ? 10 : everyPage;  //默认每页10条
	}

	private static int getCurrentPage(int currentPage) {
		return currentPage == 0 ? 1 : currentPage;
	}

	private static int getTotalPage(int everyPage, long totalCount) {
		return (int) (totalCount % everyPage == 0 ? totalCount / everyPage
				: totalCount / everyPage + 1);
	}

	private static int getBeginIndex(int everyPage, int currentPage) {
		return (currentPage - 1) * everyPage;
	}

	private static boolean getHasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}

	private static boolean getHasNextPage(int currentPage, int totalPage) {
		return currentPage < totalPage ? true : false;
	}
}
